﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

//using System.Data.Entity;

namespace G28BS.Models
{
    public enum GameResults
    {
        // Results options for a Game
        Won,
        Lost,
        Draw,
        Score,
        WonWScore,
        LostWScore
    }
    public class GameResult
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int id { get; set; }
        public int playerUserId { get; set; }

        [ForeignKey("playerUserId")]
        public UserProfile player { get; set; }

        public int gameTypeGameID { get; set; }

        [ForeignKey("gameTypeGameID")]
        public Game gameType { get; set; }

        public int score { get; set; }
        public GameResults result { get; set; }
        public string gameEnded { get; set; }
        public int gameRoomId { get; set; }
    }

    public class tttResult
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int tttResultID {get; set;}

        public bool matchDraw { get; set; }
        public bool player1Won { get; set; }
        public int player1ID { get; set; }
        public int player2ID { get; set; }

        public string gameEnd { get; set; }

        public int pointChangeValue { get; set; }

        public tttResult(int p1ID, int p2ID, bool draw)
        {
            // Declare an integer for the point change:
            int pointChange = 0;

            // Make a connection to the database:
            AppRepository repo = new AppRepository();

            // Search up user profiles
            UserProfile p1 = (from up in repo.userProfiles 
                              where up.UserId == p1ID 
                              select up).Single();
            UserProfile p2 = (from up in repo.userProfiles 
                              where up.UserId == p2ID 
                              select up).Single();

            // Load player 1 rank points:
            RankmkII p1Rank;
            try
            {
                p1Rank = (from Rank in repo.rankIIs 
                          where Rank.rankingId == p1ID 
                          select Rank).First();
            }
            // If no rank for this user, catch that exception and 
            // add new profile to rank table.
            catch (InvalidOperationException)
            {
                p1Rank = new RankmkII(p1);
                repo.AddRankII(p1Rank);
            }

            // Load player 2 rank points:
            RankmkII p2Rank;
            try
            {
                p2Rank = (from Rank in repo.rankIIs 
                          where Rank.rankingId == p2ID 
                          select Rank).First();
            }
            // If no rank for this user, catch that exception and 
            // add new profile to rank table.
            catch (InvalidOperationException)
            {
                p2Rank = new RankmkII(p2);
                repo.AddRankII(p2Rank);
            }


            if (draw == true)
            {
                // Average the 2 rank points 
                pointChange = (p1Rank.tttRank + p2Rank.tttRank) / 2;

                // Move both player ranks 1% to the average:

                int changeToP1Rank = (p1Rank.tttRank - pointChange) / 100;
                p1Rank.changeTTTRank(-changeToP1Rank);
                int changeToP2Rank = (p2Rank.tttRank - pointChange) / 100;
                p2Rank.changeTTTRank(-changeToP2Rank);

                // Fill out specific gameResults:
                this.matchDraw = true;
                this.player1Won = false;
                this.pointChangeValue = Math.Abs(changeToP1Rank);

            }
            else if (draw == false)
            {
                // winner gets all of losers points / 100 (or some percentage)
                // If the game is not a draw, the first parameter is the userProfileID of the winner.

                player1Won = true;

                pointChange = p2Rank.tttRank / 100;
                p1Rank.changeTTTRank(pointChange);
                

                // Loser loses all of his points / 100 (or some, same, percentage)
                p2Rank.changeTTTRank(-pointChange);

                // Fill out specific gameResults:
                this.matchDraw = false;
                this.player1Won = true;
                this.pointChangeValue = pointChange;

                
            }
            // Fill out the gameResults details:

            this.player1ID = p1ID;
            this.player2ID = p2ID;
            this.gameEnd = DateTime.Now.ToString();

            repo.AddtttResult(this);
            repo.Save();
        }
    }
}

